package org.finesys.chat.core.base.document.splitter;

import org.finesys.chat.core.base.document.tokenizer.Tokenizer;
import org.finesys.common.core.util.ValidationUtil;

public class DocumentByRegexSplitter extends HierarchicalDocumentSplitter {
    private final String regex;
    private final String joinDelimiter;

    public DocumentByRegexSplitter(String regex, String joinDelimiter, int maxSegmentSizeInChars, int maxOverlapSizeInChars) {
        super(maxSegmentSizeInChars, maxOverlapSizeInChars, (Tokenizer) null, (DocumentSplitter) null);
        this.regex = (String) ValidationUtil.ensureNotNull(regex, "regex");
        this.joinDelimiter = (String) ValidationUtil.ensureNotNull(joinDelimiter, "joinDelimiter");
    }

    public DocumentByRegexSplitter(String regex, String joinDelimiter, int maxSegmentSizeInChars, int maxOverlapSizeInChars, DocumentSplitter subSplitter) {
        super(maxSegmentSizeInChars, maxOverlapSizeInChars, (Tokenizer) null, subSplitter);
        this.regex = (String) ValidationUtil.ensureNotNull(regex, "regex");
        this.joinDelimiter = (String) ValidationUtil.ensureNotNull(joinDelimiter, "joinDelimiter");
    }

    public DocumentByRegexSplitter(String regex, String joinDelimiter, int maxSegmentSizeInTokens, int maxOverlapSizeInTokens, Tokenizer tokenizer) {
        super(maxSegmentSizeInTokens, maxOverlapSizeInTokens, tokenizer, (DocumentSplitter) null);
        this.regex = (String) ValidationUtil.ensureNotNull(regex, "regex");
        this.joinDelimiter = (String) ValidationUtil.ensureNotNull(joinDelimiter, "joinDelimiter");
    }

    public DocumentByRegexSplitter(String regex, String joinDelimiter, int maxSegmentSizeInTokens, int maxOverlapSizeInTokens, Tokenizer tokenizer, DocumentSplitter subSplitter) {
        super(maxSegmentSizeInTokens, maxOverlapSizeInTokens, tokenizer, subSplitter);
        this.regex = (String) ValidationUtil.ensureNotNull(regex, "regex");
        this.joinDelimiter = (String) ValidationUtil.ensureNotNull(joinDelimiter, "joinDelimiter");
    }

    public String[] split(String text) {
        return text.split(this.regex);
    }

    public String joinDelimiter() {
        return this.joinDelimiter;
    }

    protected DocumentSplitter defaultSubSplitter() {
        return null;
    }
}
